package cn.ava.pet.controller;

import cn.ava.pet.domain.Pet;
import cn.ava.pet.service.IPetTypeService;
import cn.ava.pet.domain.PetType;
import cn.ava.pet.query.PetTypeQuery;
import cn.ava.basic.utils.AjaxResult;
import cn.ava.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: PetTypeController
 * @Description: 宠物类型管理
 * @Author: Avavaava
 * @Date: 2022/7/26 16:43
 * @version: 23.53
 *
 */
@RestController
@RequestMapping("/petType")
public class PetTypeController {
    @Autowired
    public IPetTypeService petTypeService;


    /**
     * 保存和修改公用的
     * @param petType  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody PetType petType){
        try {
            if( petType.getId()!=null)
                petTypeService.update(petType);
            else
                petTypeService.add(petType);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace(); 
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petTypeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public PetType getById(@PathVariable("id")Long id)
    {
        return petTypeService.getById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<PetType> list(){
        return petTypeService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping()
    public PageList<PetType> json(@RequestBody PetTypeQuery query)
    {
        return petTypeService.queryData(query);
    }

    /**
     * @Title: getTree
     * @Description: 获取宠物类型树数据
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/16 17:16
     * @param:
     * @return:那么
     */
    @GetMapping("/tree")
    public List<PetType> getTree() {
        return petTypeService.getTree();
    }


    @PatchMapping
    public AjaxResult batchDelete(@RequestBody Long[] id) {
        try {
            petTypeService.batchDelete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }


}
